var line_height = 40;
var student_list = new Vue({
	el: '#student_list',
	data: {
		delete_student_modal_show: false,
		add_student_modal_show: false,
		add_student_comment_modal_show: false,
		add_student_comment_student_name: 'ssss',
		show: false,
	},
	methods: {
		deleteStudentModal: function (teacher_id, student_id) {
			this.$set('delete_student_modal_show', true)
			this.$set('delete_student', student_id)
			this.$data.teacher_id = teacher_id
		},
		addStudentModal: function (teacher_id, student_id) {
			this.$data.add_student_modal_show = true
			this.$data.add_student = student_id
			this.$data.teacher_id = teacher_id
		},
		addStudentCommentModal: function (teacher_id, student_id, student_name) {
			this.$data.add_student_comment_modal_show = true
			this.$data.add_student_comment_teacher = teacher_id
			this.$data.add_student_comment_student = student_id
			this.$data.add_student_comment_student_name = student_name
		},
		deleteStudent: function (bDelete) {
			this.$set('delete_student_modal_show', false)
			if (bDelete) {
				Vue.http.headers.common['X-CSRFToken'] = Cookies.get('csrftoken')
				Vue.http.post('/students/delete', {
					student_id: this.$data.delete_student,
					teacher_id: this.$data.teacher_id,
				}, {
					emulateJSON: true
				}).then(function (res) {
					window.location.reload()
				}, function (err) {})
			}
		},
		addStudent: function (bAdd) {
			this.$data.add_student_modal_show = false
			if (bAdd) {
				this.$http.get('/teacher/add-student', {
					params: {
						student_id: this.$data.add_student,
						teacher_id: this.$data.teacher_id
					}
				}).then(function (res) {
					window.location.reload()
				}, function (err) {})
			}
		},
		add_student_comment: function () {
			var score = this.$data.comment_score
			var textbook = this.$data.comment_textbook
			var content = this.$data.comment_content

			if (score == 'undefined' || score.length < 1
				|| textbook == 1
				|| content == 'undefined' || content.length < 1) {
				return;
			}

			this.$data.add_student_comment_modal_show = false
			Vue.http.headers.common['X-CSRFToken'] = Cookies.get('csrftoken')
			Vue.http.post('/students/add-comment', {
				student_id: this.$data.add_student_comment_student,
				teacher_id: this.$data.add_student_comment_teacher,
				score: score,
				textbook: textbook,
				content: content,
			}, {
				emulateJSON: true
			}).then(function (res) {
				// window.location.reload()
			}, function (err) {})
		}
	}
})

var class_table = new Vue({
	el: '#class_table',
	data: {
		weekdays: ['周日', '周一', '周二', '周三', '周四', '周五', '周六'],
		colors: ["#E94554", "#FFBB48", "#03B10A", "#25D2FF", "#187BFE", "#0037C8", "#945AFC", "#FF99FF", "#FD6991"],
		times: [],
		courses: [{
			'name': '吴新庭',
			'weekday': 1,
			'begin': '16:30',
			'duration': 0.5,
		},{
			'name': '吴新庭',
			'weekday': 2,
			'begin': '16:45',
			'duration': 0.75,
		}],
		formated_courses: [
		],
		student_info_modal_show: false,
		student_info_modal_class: {},
		duration_disabled: true,
		begin_disabled: true,
		weekday_disabled: true,
		delete_show: false,
		delete_pending: false,
	},
	methods: {
		clickStudent: function (weekday, time, course) {
			var c = this.$data.formated_courses[weekday][time][course];
			this.$data.student_info_modal_class = c
			this.$data.student_info_modal_show = true
		},
		complete: function (pk) {
			this.$data.student_info_modal_show = false
			this.$data.delete_pending = false
			var begin_time = this.$data.class_begin_time
			var duration = this.$data.class_duration
			var weekday = this.$data.class_weekday

			if (begin_time == this.$data.student_info_modal_class.fields.begin 
				&& duration == this.$data.student_info_modal_class.fields.duration * 60
				&& weekday == this.$data.student_info_modal_class.fields.weekday) {
			} else {
				begin_time = begin_time.split(':').slice(0, 2).join(':')
				Vue.http.headers.common['X-CSRFToken'] = Cookies.get('csrftoken')
				Vue.http.post('/students/update-class', {
					class_id: pk,
					begin: begin_time,
					duration: parseInt(duration) / 60,
					weekday: parseInt(weekday)
				}, {
					emulateJSON: true
				}).then(function (res) {
					window.location.reload()
				}, function (err) {})
			}
		},
		editClass: function () {
			if (!this.$data.delete_show) {
				this.$data.duration_disabled = false
				this.$data.begin_disabled = false
				this.$data.weekday_disabled = false
				this.$data.delete_show = true
			} else {
				this.$data.duration_disabled = true
				this.$data.begin_disabled = true
				this.$data.weekday_disabled = true
				this.$data.delete_show = false
				this.$data.delete_pending = false

				this.$data.class_begin_time = this.$data.student_info_modal_class.fields.begin
				this.$data.class_duration = this.$data.student_info_modal_class.fields.duration * 60
				this.$data.class_weekday = this.$data.student_info_modal_class.fields.weekday
			}
		},
		deleteClass: function (pk) {
			if (!this.$data.delete_pending) {
				this.$data.delete_pending = true
			} else {
				this.$data.delete_pending = false
				Vue.http.headers.common['X-CSRFToken'] = Cookies.get('csrftoken')
				Vue.http.post('/students/delete-class', {
					class_id: pk,
				}, {
					emulateJSON: true
				}).then(function (res) {
					window.location.reload()
				}, function (err) {})
			}
		}
	}
})

function update_tab (index) {
	class_table.$set('show', index == 0)
	student_list.$set('show', index == 1)
}

update_tab(0)

var navbar = new Vue({
	el: '#navbar',
	data: {
		tabs: ['课程', '学生'],
		brand_url: '/school',
		current: 0,
	},
	methods: {
		clickTabs: function (index) {
			this.$set('current', index)
			update_tab(index)
			Cookies.set('current', index)
		}
	}
})

function init_times() {
	var times = [];
	for (var i = 7; i <= 22; i++) {
		times.push(i)
	}
	class_table.$set('times', times)
}
init_times();

function format_courses(courses) {
	var formated_courses = [];
	for (var i = 0; i < class_table.$data.weekdays.length; i++) {
		var time_courses = [];
		for (var j = 0; j < class_table.$data.times.length; j++) {
			time_courses.push([]);
		}
		formated_courses.push(time_courses);	
	} 
	for (var i = 0; i < courses.length; i++) {
		var c = courses[i];
		var weekday = c['fields']['weekday'];

		var timearr = c['fields']['begin'].split(":");
		var hour = parseInt(timearr[0]) - 7;
		var min = parseInt(timearr[1])
		var index = Math.floor((hour + (min / 60)));
		c['top'] = (min / 60) * line_height;

		var durationarr = c['fields']['duration'].split(":");
		var dhour = parseInt(durationarr[0])
		var dmin = parseInt(durationarr[1])
		c['height'] = (dhour + dmin/60) * line_height;
		c['fields']['duration'] = dhour + dmin / 60;

		formated_courses[weekday][index].push(c);
	}
	class_table.$data.formated_courses = formated_courses;
}
// format_courses(class_table.$data.courses);

Vue.http.get('/teacher/classes', {
	params: {
		teacher_id: _P('teacher_id'),
		school_id: _P('school_id')
	}
}).then(function(res) {
	var classes = res.data['classes'];
	format_courses(classes);
	add_time_cursor();
}, function(res) {

})

function add_time_cursor() {
	date = new Date();
	weekday = date.getDay();
	hour = date.getHours();
	min = date.getMinutes();

	if (hour < 7 || hour > 22) {
		hour = 22; min = 59;
	}

	select_str = '#course tbody tr:eq(' + (hour - 7) + ') td:eq(' + (weekday + 1) + ')';
	$('#cursor').css('top', ((min / 60) * line_height));
	$(select_str).append($('#cursor'));
}

setInterval(add_time_cursor, 1000 * 60 * 10) // 10 min